<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2011 Florian Müller <it-admin@ifp-potsdam.co>, IFP
*  
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 3 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
 * Controller for the Kurs object
 */
class Tx_IfpCurriculum2_Controller_KursController extends Tx_Extbase_MVC_Controller_ActionController {


	/**
	 * @var Tx_IfpCurriculum2_Domain_Repository_KursRepository
	 */
	protected $kursRepository;
/**
	 * @param Tx_IfpCurriculum2_Domain_Repository_KursRepository $kursRepository
 	 * @return void
-	 */
	public function injectKursRepository(Tx_IfpCurriculum2_Domain_Repository_KursRepository $kursRepository) {
		$this->kursRepository = $kursRepository;
	}
	/**
	 * Displays a single Kurs
	 *
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $kurs the Kurs to display
	 * @return string The rendered view
	 */
	public function showAction(Tx_IfpCurriculum2_Domain_Model_Kurs $kurs) {
		
		$semester=$kurs->getKursstamm()->getTurnuso()->getNextSemester();
		$veranstaltungRepository = t3lib_div::makeInstance('Tx_IfpCurriculum2_Domain_Repository_VeranstaltungRepository');
		$veranstaltungen=$veranstaltungRepository->getByKursInFuture($kurs);
		$this->view->assign('semester', $semester);
		$this->view->assign('veranstaltungen', $veranstaltungen);
		$this->view->assign('kurs', $kurs);
	}

/**
	 * Displays a single Kurs
	 *
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $kurs the Kurs to display
	 * @return string The rendered view
	 */
	public function adminshowAction(Tx_IfpCurriculum2_Domain_Model_Kurs $kurs) {
		$this->showAction($kurs);
	}
	/**
	 * Displays a form for creating a new  Kurs
	 *@param Tx_IfpCurriculum2_Domain_Model_Kursstamm $kursstamm
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $newKurs a fresh Kurs object which has not yet been added to the repository
	 * @return void
	 * @dontvalidate $newKurs
	 */
	public function newAction(Tx_IfpCurriculum2_Domain_Model_Kursstamm $kursstamm,Tx_IfpCurriculum2_Domain_Model_Kurs $newKurs = NULL) {
		$dozentRepository = t3lib_div::makeInstance('Tx_IfpCurriculum2_Domain_Repository_DozentRepository');
		$dozenten=$dozentRepository->findAll();
		$dozentenarray=array();
		$dozentenarray[]="";
		foreach ($dozenten as $dozent) {
				$dozentenarray[]=$dozent;
		}
		
		
		$this->view->assign('dozenten', $dozentenarray);
		$this->view->assign('newKurs', $newKurs);
		$this->view->assign('kursstamm', $kursstamm);
		
	}
	/**
	 * Displays a form for creating a new  Kurs
	
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $kurs a fresh Kurs object which has not yet been added to the repository
	 * @return void
	 * @dontvalidate $newKurs
	 */
	public function newversionAction(Tx_IfpCurriculum2_Domain_Model_Kurs $kurs) {
		$dozentRepository = t3lib_div::makeInstance('Tx_IfpCurriculum2_Domain_Repository_DozentRepository');
		$dozenten=$dozentRepository->findAll();
		$dozentenarray=array();
		$dozentenarray[]="";
		foreach ($dozenten as $dozent) {
				$dozentenarray[]=$dozent;
		}
		$newkurs= new Tx_IfpCurriculum2_Domain_Model_Kurs();
		$newkurs->setKursstamm($kurs->getKursstamm());
		$newkurs->setVersion($kurs->getKursstamm()->getNextVersion());
		$newkurs->setBeschreibung($kurs->getBeschreibung());
		$newkurs->setDozent($kurs->getDozent());
		$newkurs->setLiteratur($kurs->getLiteratur());
		$newkurs->setTitel($kurs->getTitel());
		$newkurs->setUe($kurs->getUe());
		$this->view->assign('dozenten', $dozentenarray);
		
		$this->view->assign('kurs', $newkurs);
		$this->view->assign('kursold', $kurs);
		
	}


	/**
	 * Creates a new Kurs and forwards to the list action.
	 *
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $newKurs a fresh Kurs object which has not yet been added to the repository
	 * @param Tx_IfpCurriculum2_Domain_Model_Kursstamm $kursstamm
	 * @return void
	 */
	public function createAction(Tx_IfpCurriculum2_Domain_Model_Kurs $newKurs,Tx_IfpCurriculum2_Domain_Model_Kursstamm $kursstamm) {
		$newKurs->setKursstamm($kursstamm);
		$kursstammRepository = t3lib_div::makeInstance('Tx_IfpCurriculum2_Domain_Repository_KursstammRepository');
		$kursstamm->addKurse($newKurs);
		$kursstammRepository->update($kursstamm);
		$this->flashMessageContainer->add('Your new Kurs was created.');
		$this->redirect('adminshow',"Kursstamm",null,array('kursstamm'=>$kursstamm));
	}
	/**
	 * Creates a new Kurs and forwards to the list action.
	 *
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $kurs a fresh Kurs object which has not yet been added to the repository
	 * @param Tx_IfpCurriculum2_Domain_Model_Kursstamm $kursstamm
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $kursold
	 * @return void
	 */
	public function createnewversionAction(Tx_IfpCurriculum2_Domain_Model_Kurs $kurs,Tx_IfpCurriculum2_Domain_Model_Kursstamm $kursstamm,Tx_IfpCurriculum2_Domain_Model_Kurs $kursold) {
		$kurs->setKursstamm($kursstamm);
		$kursstammRepository = t3lib_div::makeInstance('Tx_IfpCurriculum2_Domain_Repository_KursstammRepository');
		$kursstamm->addKurse($kurs);
		$kursold->setInaktiv(true);
		$this->kursRepository->update($kursold);
		$kursstammRepository->update($kursstamm);
		$this->flashMessageContainer->add('Your new Kurs was created.');
		$this->redirect('adminshow',"Kursstamm",null,array('kursstamm'=>$kursstamm));
	}


	
	/**
	 * Displays a form for editing an existing Kurs
	 *
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $kurs the Kurs to display
	 * @return string A form to edit a Kurs 
	 */
	public function editAction(Tx_IfpCurriculum2_Domain_Model_Kurs $kurs) {
		$dozentRepository = t3lib_div::makeInstance('Tx_IfpCurriculum2_Domain_Repository_DozentRepository');
		$dozenten=$dozentRepository->findAll();
		$dozentenarray=array();
		$dozentenarray[]="";
		foreach ($dozenten as $dozent) {
				$dozentenarray[]=$dozent;
		}
		
		
		$this->view->assign('dozenten', $dozentenarray);
		$this->view->assign('kurs', $kurs);
	}



	/**
	 * Updates an existing Kurs and forwards to the list action afterwards.
	 *
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $kurs the Kurs to display
	 */
	public function updateAction(Tx_IfpCurriculum2_Domain_Model_Kurs $kurs) {
		
		$this->kursRepository->update($kurs);
		$this->flashMessageContainer->add('Your Kurs was updated.');
		$this->redirect('adminshow',"Kursstamm",null,array('kursstamm'=>$kurs->getKursstamm()));
	}
/**
	 * Updates an existing Kurs and forwards to the list action afterwards.
	 *
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $kurs the Kurs to display
	 */
	public function togglestatusAction(Tx_IfpCurriculum2_Domain_Model_Kurs $kurs) {
		$kurs->setInaktiv(!$kurs->isInaktiv());
		$this->kursRepository->update($kurs);
		$this->flashMessageContainer->add('Your Kurs was updated.');
		$this->redirect('adminshow',"Kurs",null,array('kurs'=>$kurs));
	}


			/**
	 * Deletes an existing Kurs
	 *
	 * @param Tx_IfpCurriculum2_Domain_Model_Kurs $kurs the Kurs to be deleted
	 * @return void
	 */
	public function deleteAction(Tx_IfpCurriculum2_Domain_Model_Kurs $kurs) {
		$this->kursRepository->remove($kurs);
		$this->flashMessageContainer->add('Your Kurs was removed.');
		$this->redirect('adminshow',"Kursstamm",null,array('kursstamm'=>$kurs->getKursstamm()));
	}


}
?>